{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 完整代码\n",
    "data_strings 为列表，\n",
    "[ \n",
    "\"日期: 2018-05-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 \",\n",
    "\"日期: 2018-06-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 \",\n",
    "\"日期: 2018-05-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 \",\n",
    "]\n",
    "\n",
    "根据逗号分为parts列表， ['日期: 2018-05-29 ', ...... ' 当前资产: 981971.97 ']\n",
    "\n",
    "提取parts列表中的每一项part，'当前资产: 981971.97 '\n",
    "\n",
    "把每个part做成字典,{'日期': '2018-05-29',...... ' 当前资产': '981971.97'}\n",
    "\n",
    "把每个字典row_data添加到data_rows列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd  \n",
    "\n",
    "data_strings = [ \"日期: 2018-05-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 \",\n",
    "                 \"日期: 2018-06-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 \",\n",
    "                 \"日期: 2018-05-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 \",\n",
    "                ]\n",
    "\n",
    "data_rows = []  \n",
    "\n",
    "for data_str in data_strings:  \n",
    "    #使用 split 方法将字符串 data_str 根据逗号 , 分割成一个列表 parts。\n",
    "    parts = data_str.split(',')  # 按逗号和空格分割字符串  \n",
    "    # 创建一个空的字典（dictionary）并将它赋值给变量 row_data。\n",
    "    row_data = {} \n",
    "    for part in parts:  \n",
    "        key_value = part.split(':')  # 尝试按冒号和空格分割键值对  \n",
    "        if len(key_value) == 2:  # 确保分割后有两个元素  \n",
    "            key, value = key_value  \n",
    "            row_data[key] = value.strip()  # 去除值两侧的空白字符  \n",
    "        else:  \n",
    "            # 处理分割后元素数量不是2的情况，例如打印警告或跳过该键值对  \n",
    "            print(f\"Warning: Invalid key-value pair: {part}\")  \n",
    "              \n",
    "    # 将处理后的字典添加到列表中  \n",
    "    data_rows.append(row_data)  \n",
    "df = pd.DataFrame(data_rows) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>执行</th>\n",
       "      <th>股票代码</th>\n",
       "      <th>成交量</th>\n",
       "      <th>成交价</th>\n",
       "      <th>当前资产</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-05-29</td>\n",
       "      <td>执行买入</td>\n",
       "      <td>000001</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1778.03</td>\n",
       "      <td>981971.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-06-29</td>\n",
       "      <td>执行买入</td>\n",
       "      <td>000001</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1778.03</td>\n",
       "      <td>981971.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-05-29</td>\n",
       "      <td>执行买入</td>\n",
       "      <td>000001</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1778.03</td>\n",
       "      <td>981971.97</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    执行    股票代码    成交量      成交价       当前资产\n",
       "0  2018-05-29  执行买入  000001  500.0  1778.03  981971.97\n",
       "1  2018-06-29  执行买入  000001  500.0  1778.03  981971.97\n",
       "2  2018-05-29  执行买入  000001  500.0  1778.03  981971.97"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_strings = [ \"日期: 2018-05-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 \",\n",
    "                 \"日期: 2018-06-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 \",\n",
    "                 \"日期: 2018-05-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 \",\n",
    "                ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd  \n",
    "  \n",
    "def parse_data_strings_to_df(data_strings):  \n",
    "    data_rows = []  \n",
    "    for data_str in data_strings:  \n",
    "        parts = data_str.split(',')  # 按逗号分割字符串  \n",
    "        row_data = {}  \n",
    "        for part in parts:  \n",
    "            key_value = part.split(':')  # 尝试按冒号分割键值对  \n",
    "            if len(key_value) == 2:  \n",
    "                key, value = key_value  \n",
    "                row_data[key.strip()] = value.strip()  # 去除键和值两侧的空白字符  \n",
    "            else:  \n",
    "                print(f\"Warning: Invalid key-value pair: {part}\")  \n",
    "        data_rows.append(row_data)  \n",
    "    return pd.DataFrame(data_rows)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=parse_data_strings_to_df(data_strings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>执行</th>\n",
       "      <th>股票代码</th>\n",
       "      <th>成交量</th>\n",
       "      <th>成交价</th>\n",
       "      <th>当前资产</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-05-29</td>\n",
       "      <td>执行买入</td>\n",
       "      <td>000001</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1778.03</td>\n",
       "      <td>981971.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-06-29</td>\n",
       "      <td>执行买入</td>\n",
       "      <td>000001</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1778.03</td>\n",
       "      <td>981971.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-05-29</td>\n",
       "      <td>执行买入</td>\n",
       "      <td>000001</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1778.03</td>\n",
       "      <td>981971.97</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    执行    股票代码    成交量      成交价       当前资产\n",
       "0  2018-05-29  执行买入  000001  500.0  1778.03  981971.97\n",
       "1  2018-06-29  执行买入  000001  500.0  1778.03  981971.97\n",
       "2  2018-05-29  执行买入  000001  500.0  1778.03  981971.97"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分解说明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd  \n",
    "data_strings = [ \"日期: 2018-05-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 \",\n",
    "                 \"日期: 2018-06-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 \",\n",
    "                 \"日期: 2018-05-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 \",\n",
    "                ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['日期: 2018-05-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 ',\n",
       " '日期: 2018-06-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 ',\n",
       " '日期: 2018-05-29 , 执行: 执行买入,股票代码: 000001, 成交量: 500.0,成交价: 1778.03, 当前资产: 981971.97 ']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_rows = []  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['日期: 2018-05-29 ', ' 执行: 执行买入', '股票代码: 000001', ' 成交量: 500.0', '成交价: 1778.03', ' 当前资产: 981971.97 ']\n",
      "['日期: 2018-06-29 ', ' 执行: 执行买入', '股票代码: 000001', ' 成交量: 500.0', '成交价: 1778.03', ' 当前资产: 981971.97 ']\n",
      "['日期: 2018-05-29 ', ' 执行: 执行买入', '股票代码: 000001', ' 成交量: 500.0', '成交价: 1778.03', ' 当前资产: 981971.97 ']\n"
     ]
    }
   ],
   "source": [
    "for data_str in data_strings:  \n",
    "    #使用 split 方法将字符串 data_str 根据逗号 , 分割成一个列表 parts。\n",
    "    parts = data_str.split(',')  # 按逗号和空格分割字符串 \n",
    "    print(parts) # ['日期: 2018-05-29 ', ...... ' 当前资产: 981971.97 ']\n",
    "    \n",
    "    row_data = {} \n",
    "    for part in parts:  # part 为 '当前资产: 981971.97 '\n",
    "        key_value = part.split(':')  # 尝试按冒号和空格分割键值对  \n",
    "                                    # key_value 为 ['当前资产', ' 981971.97 ']\n",
    "        # 如果 key_value 包含两个元素，这行代码将它们解包到 key 和 value 变量中。                            \n",
    "        if len(key_value) == 2:  # 确保分割后有两个元素 ，第一个元素是键，第二个元素是值。 \n",
    "            key, value = key_value  # key 为 '当前资产'， value 为  ' 981971.97 '\n",
    "            row_data[key] = value.strip()  #将 value 字符串两侧的空白字符（如空格、制表符或换行符）删除  \n",
    "        else:  \n",
    "            # 处理分割后元素数量不是2的情况，例如打印警告或跳过该键值对  \n",
    "            print(f\"Warning: Invalid key-value pair: {part}\")  \n",
    "              \n",
    "    # 将处理后的字典添加到列表中  \n",
    "    data_rows.append(row_data) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[' 当前资产', ' 981971.97 ']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "key_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' 当前资产'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' 981971.97 '"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' 当前资产: 981971.97 '"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'日期': '2018-05-29',\n",
       " ' 执行': '执行买入',\n",
       " '股票代码': '000001',\n",
       " ' 成交量': '500.0',\n",
       " '成交价': '1778.03',\n",
       " ' 当前资产': '981971.97'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'981971.97'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row_data[key]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           日期    执行    股票代码    成交量      成交价       当前资产\n",
      "0  2018-05-29  执行买入  000001  500.0  1778.03  981971.97\n",
      "1  2018-06-29  执行买入  000001  500.0  1778.03  981971.97\n",
      "2  2018-05-29  执行买入  000001  500.0  1778.03  981971.97\n"
     ]
    }
   ],
   "source": [
    "# 创建一个DataFrame  \n",
    "df = pd.DataFrame(data_rows)  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>执行</th>\n",
       "      <th>股票代码</th>\n",
       "      <th>成交量</th>\n",
       "      <th>成交价</th>\n",
       "      <th>当前资产</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-05-29</td>\n",
       "      <td>执行买入</td>\n",
       "      <td>000001</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1778.03</td>\n",
       "      <td>981971.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-06-29</td>\n",
       "      <td>执行买入</td>\n",
       "      <td>000001</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1778.03</td>\n",
       "      <td>981971.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-05-29</td>\n",
       "      <td>执行买入</td>\n",
       "      <td>000001</td>\n",
       "      <td>500.0</td>\n",
       "      <td>1778.03</td>\n",
       "      <td>981971.97</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    执行    股票代码    成交量      成交价       当前资产\n",
       "0  2018-05-29  执行买入  000001  500.0  1778.03  981971.97\n",
       "1  2018-06-29  执行买入  000001  500.0  1778.03  981971.97\n",
       "2  2018-05-29  执行买入  000001  500.0  1778.03  981971.97"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 打印DataFrame查看结果  \n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "日期: 2018-05-29 \n",
      " 执行: 执行买入\n",
      "股票代码: 000001\n",
      " 成交量: 500.0\n",
      "成交价: 1778.03\n",
      " 当前资产: 981971.97 \n",
      "日期: 2018-06-29 \n",
      " 执行: 执行买入\n",
      "股票代码: 000001\n",
      " 成交量: 500.0\n",
      "成交价: 1778.03\n",
      " 当前资产: 981971.97 \n",
      "日期: 2018-05-29 \n",
      " 执行: 执行买入\n",
      "股票代码: 000001\n",
      " 成交量: 500.0\n",
      "成交价: 1778.03\n",
      " 当前资产: 981971.97 \n"
     ]
    }
   ],
   "source": [
    "for data_str in data_strings:  \n",
    "    #使用 split 方法将字符串 data_str 根据逗号 , 分割成一个列表 parts。\n",
    "    parts = data_str.split(',')  # 按逗号和空格分割字符串 \n",
    "    #print(parts)\n",
    "    \n",
    "    row_data = {} \n",
    "    for part in parts:  \n",
    "        print(part)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
